// xp_loadlibrary.h
// ----------------
// Example extended stored procedure DLL for loading libraries (other DLLs)
// out of a database table.
//
// This example makes use of the MemoryModule library located here:
// https://github.com/fancycode/MemoryModule
// The files have been slightly modified to use strict multi-byte, as it had
// some conflicts when introduced into a TCHAR project that could swap over
// to UNICODE.  A copy of the original source files, including the license
// can be found in the archive of this project.

#pragma once

// Required libs
#pragma comment(lib,"C:\\Program Files\\Microsoft SQL Server\\90\\SDK\\Lib\\x86\\opends60.lib")
//                      ^^ this is the default path - adjust as needed if yours differs ^^

#define _CRT_SECURE_NO_WARNINGS // Disable warnings about non *_s functions
#define WIN32_LEAN_AND_MEAN

#include <Windows.h>
#include <stdlib.h>
#include <sqlext.h>
#include <sql.h>
#include <sqlext.h>
#include <odbcss.h>
#include <tchar.h>
#include <srv.h>

#include "MemoryModule.h"

#define TACOS_RULE				1
#define RESULTS_MAX_COL_SIZE	8000		// Max column size for results
#ifndef XP_NOERROR
#define XP_NOERROR				0			// Used for Sql_Send* functions
#endif
#ifndef XP_ERROR
#define XP_ERROR				1			// Used for Sql_Send* functions
#endif

// Globals
DWORD	dwTOTALROWS = 0;

// Exports
#ifdef __cplusplus
extern "C"
{
	#endif
	__declspec(dllexport) ULONG __GetXpVersion();
	#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C"
{
	#endif
	__declspec(dllexport) SRVRETCODE xp_loadlibrary(SRV_PROC *srvproc);
	#ifdef __cplusplus
}
#endif

// Typedefs
typedef bool (*DLLFromMemory)(SRV_PROC *srvproc, TCHAR* tcParameters);

// ^^ Be sure you know your DLL function inputs/outputs

// Functions
void		PrintHelpInfo		(SRV_PROC *srvproc);
bool		GetPfieldInfo		(SRV_PROC *srvproc,int iField,TCHAR** tcBuffer);
bool		GetParamInfo		(SRV_PROC *srvproc,int iParam,TCHAR** tcBuffer);
bool		NormalizeSqlData	(void* pbData,TCHAR** tczClientRequest,ULONG pcbActualLen,BYTE pbType);
SRVRETCODE	SqlLoadLibrary		(SRV_PROC *srvproc);
SRVRETCODE	Sql_SendMsg			(SRV_PROC *srvproc,TCHAR* tczMsg);
SRVRETCODE	Sql_SendErr			(SRV_PROC *srvproc,TCHAR* tczMsg);